//
//  StringHelper.m
//  LubiEdu
//
//  Created by chuanglong03 on 16/6/30.
//  Copyright © 2016年 chuanglong. All rights reserved.
//

#import "StringHelper.h"

@implementation StringHelper

#pragma mark - 判断字符串是否全部为空格
+ (BOOL)judgeAllSpace:(NSString *)string {
    BOOL allSapce = NO;
    // 获取被空格分割后是空的字符串的个数
    NSInteger number = 0;
    NSArray *array = [string componentsSeparatedByString:@" "];
    for (NSString *str in array) {
        if ([str isEqualToString:@""]) {
            number++;
        }
    }
    // 如果空的字符串的个数等于 string.length+1，就说明全部是空格
    if (number == string.length+1) {
        allSapce = YES;
    }
    return allSapce;
}

#pragma mark - 判断字符串是否为空或者全部为空格
+ (BOOL)judgeEmptyOrAllSpace:(NSString *)string {
    BOOL emptyOrAllSpace = NO;
    if (string.length == 0 || [self judgeAllSpace:string]) {
        emptyOrAllSpace = YES;
    }
    return emptyOrAllSpace;
}

#pragma mark - 判断手机号是否有效
+ (BOOL)judgeValidMobile:(NSString *)mobile {
    if (mobile.length != 11) {
        return NO;
    } else {
        /**
         * 移动号段正则表达式
         */
        NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
        /**
         * 联通号段正则表达式
         */
        NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
        /**
         * 电信号段正则表达式
         */
        NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
        NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
        BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
        NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
        BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
        NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
        BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
        if (isMatch1 || isMatch2 || isMatch3) {
            return YES;
        } else {
            return NO;
        }
    }
}

#pragma mark - 判断字符串是否为数字或字母
+ (BOOL)judgeNumberOrLetter:(NSString *)string {
    const char *characterChars = [string cStringUsingEncoding:NSUTF8StringEncoding];
    if (strlen(characterChars) > 0) {
        for (NSInteger i = 0; i < strlen(characterChars); i++) {
            char character = characterChars[i];
            if (![self judgeNumber:character] && ![self judgeLetter:character]) {
                return NO;
            }
        }
        return YES;
    } else {
        return NO;
    }
}

#pragma mark - 判断字符是否为数字
+ (BOOL)judgeNumber:(char)character {
    if (character >= '0' && character <= '9') {
        return YES;
    }
    return NO;
}

#pragma mark - 判断字符是否为字母
+ (BOOL)judgeLetter:(char)character {
    if ((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z')) {
        return YES;
    }
    return NO;
}

+ (NSString *)jsonStringFromObject:(id)obj {
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:obj options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];;
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    NSRange range = {0,jsonString.length};
    //去掉字符串中的空格
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    NSRange range2 = {0,mutStr.length};
    //去掉字符串中的换行符
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    //    NSRange range3 = {0,mutStr.length};
    //    [mutStr replaceOccurrencesOfString:@"\"" withString:@"" options:NSLiteralSearch range:range3];
    return mutStr;
}

@end
